<?php

class BzAction extends Action {


  function _initialize() {
    $this->assign("istp", 1);//circle-header2中有TP和PHPWIND都用到，加上这个参数进行区别
  }


  public function index() {
    $id = $_GET['id'];
    $m = M();
    $sql = "select t1.*,t2.type_letter,t2.type_name from bz t1 left join bz_type t2 on (t1.type_id = t2.id) where t1.id = $id";
    $bz = $m->query($sql);
    $bz = $bz[0];
    //处理一下数据，方便显示
    $bz['add_time'] = date("Y-m-d h:i:s", $bz['add_time']);
    $bz['file_level'] = ($bz['file_level'] == 0)?"":$bz['file_level'];
    $postfix = explode(".", $bz['file_url']);
    $bz["postfix"] = $postfix[count($postfix)-1];
    $sql = "select count(*) as num,t1.type_id ,t2.type_name from bz t1 left join bz_type t2 on(t1.type_id=t2.id) group by t1.type_id";
    $typeList = $m->query($sql);
    $sql = "select * from bz order by add_time desc limit 0,10";
    $newBz = $m->query($sql);
    $sql = "select * from a_doc order by add_time desc limit 0,10";
    $newDoc = $m->query($sql);
    $this->assign("bz", $bz);
    $this->assign("type_list",$typeList);
    $this->assign("new_bz", $newBz);
    $this->assign("new_doc", $newDoc);
    $this->display();
  }

  public function bzList() {
    $typeID = $_GET["type_id"];
    $m = M("Bz");
    $count = $m->where("type_id=$typeID")->count();
    import("@.ORG.Page"); //导入分页类
    $records = 30;//每页多少条记录
    $p = new Page ($count,$records);
    $p->setConfig('header','篇记录');
    $p->setConfig('prev',"上一页");
    $p->setConfig('next','下一页');
    $p->setConfig('first','<<');
    $p->setConfig('last','>>');
    $page = $p->show ();
    $list = $m->where("type_id=$typeID")->order("add_time desc")->limit($p->firstRow.",".$p->listRows)->select();
    $this->assign("count", $count);
    $this->assign("resultList", $list);
    $this->assign("nowPage",$p->nowPage);
    $this->assign("page", $page );
    $this->assign("records",$records);
    $sql = "select t1.id,t1.type_name,ifnull(t2.sum,0) as sum from bz_type t1 left join (select count(*) as sum,type_id from bz group by type_id) as t2 on (t1.id=t2.type_id) ";
    $typeList = $m->query($sql);
    $this->assign("type_list", $typeList);
    $this->display('bz-list');
  }

  public function downLoad() {
    $id = $_POST["id"];
    if (!$id || $id == 0 || $id == "") $this->ajaxReturn($id, "你要下载的文件不存在", '3');
    $url = $this->checkCookie();
    if ($url != "") $this->ajaxReturn($url, '你还没有登陆，请登录', 2);
    $m = M("Bz");
    $url = $m->field("file_url")->where("id=$id")->find();
    $url = str_replace("m_bz", "mod_bz", $url["file_url"]);
    $url = str_replace("bz_complete", "bz_complete/", $url);
    $this->ajaxReturn($url, "成功", 1);
  }

  private function checkCookie() {
    $this->_dev = C("DEV_FLAG");
    $url = "http://localhost/www2/pw8/login.php";
    if ($this->_dev == 0) $url = "http://www.ehs.cn/pw8/login.php";
    if(!$_COOKIE['tpxslmlusername'] and !$_COOKIE['tpxslmluserid'] and !$_COOKIE['tpxslmlgroupid'] and !$_COOKIE['tpxslmlrnd']) {
      //return "";
      return $url;
    }else return "";
  }
}
?>
